package com.yrihr.wisdom.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yrihr.wisdom.entity.River;
import com.yrihr.wisdom.mapper.RiverMapper;
import com.yrihr.wisdom.service.RiverService;
import com.yrihr.wisdom.vo.RiverVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName : RiverServiceImpl
 * @Description :
 * @Author : wind
 * @Date: 2020-12-17 15:13
 * @Version 1.0
 */
@Service
@RequiredArgsConstructor
public class RiverServiceImpl extends ServiceImpl<RiverMapper, River> implements RiverService {

    @Override
    public Integer insertRiver(List<River> list) {
        return baseMapper.insertRiver(list);
    }

    /**
     * 根据站点名称和区间编号查询站点id
     * @param id
     * @param siteName
     * @return
     */
    @Override
    public Long selectSiteId(Long id,String siteName) {
        QueryWrapper<River> wrapper = Wrappers.query();
        wrapper.eq("type",id);
        wrapper.eq("name",siteName);
        wrapper.eq("status","1");
        return baseMapper.selectSiteId(wrapper);
    }

    /**
     * 带条件的分页查询河道信息
     *
     * @param current
     * @param limit
     * @param river
     * @return
     */
    @Override
    public IPage<River> selectPage(long current, long limit, RiverVo river) {
        QueryWrapper<River> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("interval_id","time");
        wrapper.orderByDesc("water_level");
        if (river != null) {
            if (river.getIntervalList() != null) {
                wrapper.in("interval_id",river.getIntervalList());
            }
            if (river.getStartTime() != null) {
                wrapper.ge("time",river.getStartTime());
            }
            if (river.getEndTime() != null) {
                wrapper.le("time",river.getEndTime());
            }

        }
        Page<River> page = new Page<River>(current, limit);
//        page = baseMapper.selectPage(page, wrapper);
        page = baseMapper.selectMyPage(page, wrapper);
        return page;
    }

    /**
     * 查询
     *
     * @param river
     * @return
     */
    @Override
    public List<River> selectRiverDownload(RiverVo river) {
        QueryWrapper<River> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("interval_id","time");
        wrapper.orderByDesc("water_level");
        if (river != null) {
            if (river.getIntervalList() != null) {
                wrapper.in("interval_id",river.getIntervalList());
            }
            if (river.getStartTime() != null) {
                wrapper.ge("time",river.getStartTime());
            }
            if (river.getEndTime() != null) {
                wrapper.le("time",river.getEndTime());
            }

        }
        List<River> rivers = baseMapper.selectRiverDownload(wrapper);
        return rivers;
    }
}
